<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleSave"> 新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '详情',
                icon: 'clarity:info-standard-line',
                onClick: handleDesc.bind(null, record),
              },
              {
                label: '编辑',
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <CollegeDesc @register="registerDescModal" />
    <CollegeEdit @register="registerEditModal" />
  </div>
</template>
<script lang="ts" setup>
  import CollegeDesc from "./Desc.vue";
  import CollegeEdit from "./Edit.vue";
  import { useModal } from "/@/components/Modal";
  import { getCollegePageApi } from "/@/api/core/college";
  import { CollegeModel } from "/@/api/model/collegeModel";
  import { basicColums } from "/@/views/root/college/data";
  import { defaultActionColumn, defaultTableSettings } from "/@/hooks/core/useDefault";
  import { BasicTable, TableAction, useTable } from "/@/components/Table";
  import { OperTypeEnum } from "/@/enums/typeEnum";

  const [registerTable] = useTable({
    title: "学院列表",
    api: getCollegePageApi,
    rowKey: "collegeId",
    columns: basicColums,
    actionColumn: {
      width: 240,
      ...defaultActionColumn,
    },
    ...defaultTableSettings,
  });

  const [registerDescModal, { openModal: openDescModal }] = useModal();
  const [registerEditModal, { openModal: openEditModal }] = useModal();

  function handleDesc(data: CollegeModel) {
    openDescModal(true, data);
  }

  function handleEdit(data: CollegeModel) {
    openEditModal(true, { data, operType: OperTypeEnum.UPDATE });
  }

  function handleSave() {
    openEditModal(true, { operType: OperTypeEnum.SAVE });
  }
</script>
